package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.bean.AboutUsBean;
import com.dingwei.superiordiary.mvp.presenter.AboutUsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.utils.html.HtmlUtils;
import com.dingwei.superiordiary.utils.html.URLImageGetter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.utils.AppManager;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/1/17.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class AboutUsActivity extends BaseActivity implements Contract.AboutUsView {

    AboutUsPresenter aboutUsPresenter = new AboutUsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.tv_content)
    WebView tvContent;
    @Bind(R.id.data_layout)
    ScrollView dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;


    private static String content = "<p>\r\n\t建华大街房间等你发链接文林街\r\n</p>\r\n<p>\r\n\t" +
            "<img src=\"https://img.alicdn.com/imgextra/i3/725677994/TB2ALE4XMn.PuJjSZFkXXc_lpXa_!!725677994.jpg\" title=\"金沙河\" alt=\"金沙河\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i4/1129326215/TB2MZ3.gFXXXXcYXXXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i4/1129326215/TB2_NkRgFXXXXcaXpXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i2/725677994/TB2ITHQhCVmpuFjSZFFXXcZApXa_!!725677994.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i4/725677994/TB27EY6hypnpuFjSZFIXXXh2VXa_!!725677994.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i1/1129326215/TB2V2JXgVXXXXcUXXXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i1/1129326215/TB2r38kgVXXXXaaXXXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i1/1129326215/TB2nz0mgVXXXXXSXXXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i4/1129326215/TB2njg.gFXXXXXfXpXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "<img src=\"https://img.alicdn.com/imgextra/i2/1129326215/TB2KatngVXXXXadXXXXXXXXXXXX_!!1129326215.jpg\" alt=\"\" />\n" +
            "\n</p>";
    @Override
    protected int getContentViewId() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void initData() {
//        aboutUsPresenter.getAboutUs();
        tvContent.loadUrl(URLS.HOST+URLS.ABOUT_US);

    }

    @Override
    protected void initView() {
        tvTitle.setText("关于我们");
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void setData(AboutUsBean bean) {
//        tvContent.setText(Html.fromHtml(content));
//        new HTMLPicUtils(tvContent).htmlWebPic(content);
//        tvContent.setHtml(content,new HtmlHttpImageGetter(tvContent));
//        tvContent.setText(HtmlUtils.getHtml(context,tvContent,content));
//        tvContent.loadDataWithBaseURL("about:blank", content, "text/html", "utf-8", null);
        tvContent.loadUrl(URLS.HOST+URLS.ABOUT_US);
        showData("success");
    }

    private void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.img_back, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.no_net_btn:
                aboutUsPresenter.getAboutUs();
                break;
        }
    }
}
